package csdj.controller;

import com.lww.springcloudapi.pojo.TabHallSetmeal;
import com.lww.springcloudapi.pojo.TabHallUserbill;
import com.lww.springcloudapi.pojo.TabHallUsermessage;
import com.lww.springcloudapi.pojo.TabHallUserpackage;
import com.lww.springcloudapi.result.LayuiEntity;
import com.lww.springcloudapi.result.Result;
import csdj.mapper.TabHallUsermassgeMapper;
import csdj.service.TabHallSetmealService;
import csdj.service.TabHallUserbillService;
import csdj.service.TabHallUsermassgeService;
import csdj.service.TabHallUserpackageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import java.util.Date;

@RestController
@RequestMapping("/hallUserPackage")
@Api(tags = "修改用户套餐信息")
//@CrossOrigin
public class TabHallUserpackageController {

    @Autowired
    private TabHallUserpackageService userpackageService;
    @Autowired
    private TabHallSetmealService setmealService;
    @Autowired
    private TabHallUserbillService userbillService;
    @Autowired
    private TabHallUsermassgeService tabHallUsermassgeService;

    @ApiOperation(value = "修改用户套餐 初始化用户使用情况 和套餐扣费")
    @RequestMapping("/updateUserSetMeal")
    @Transactional
    public LayuiEntity updateUserSetMeal(TabHallUserpackage userpackage) {
        LayuiEntity layuiEntity = new LayuiEntity();
        try {
            //从redis得到电话号码 wsl写的 单独用报错
            Jedis jedis = new Jedis("localhost");
            String phone = jedis.get("user");
            userpackage.setPnumber(phone);

            Double money = userpackageService.selectUserPackageByPhone(phone).getMoney();
            //计算需要修改的余额（根据用户表套餐id 查询到套餐价格 最后计算）
            //1.得到页面传过来的用户详细实体的套餐id
            Integer id = userpackage.getSetmealid();
            //2.根据用户详细的套餐id查询到套餐实体
            TabHallSetmeal setmeal = setmealService.selectSetmeal(id);
            //3.然后把套餐实体的余额减去套餐的金额 （并把套餐，通话时长，信息初始化 --待定）
            userpackage.setConversationtime(setmeal.getConversationtime());
            userpackage.setFlow(setmeal.getFlow());
            userpackage.setMessage(setmeal.getMessage());
            //修改后余额 = 号码信息.余额 - 套餐信息.金额

            userpackage.setMoney(money- setmeal.getMoney());
            //4.最后修改
            Integer count = userpackageService.updateUserSetMeal(userpackage);

            //修改用户套餐信息
            TabHallUsermessage tabHallUsermessage=new TabHallUsermessage();
            tabHallUsermessage.setPnumber(phone);
            tabHallUsermessage.setSetmealid(id);
            System.out.println(tabHallUsermessage.getSetmealid());
            tabHallUsermassgeService.updatmassge(tabHallUsermessage);

            //添加一条套餐扣费
            TabHallUserbill userbill = new TabHallUserbill();
            userbill.setPnumber(userpackage.getPnumber());
            userbill.setDeduction(setmeal.getMoney());
            userbill.setOperationtime(new Date());
            userbill.setOperation("套餐扣费");

            System.out.println("===========>" + userbillService.insertUserBill(userbill));

            //包装返回
            layuiEntity.setData(count);
            layuiEntity.setMsg("用户套餐修改成功");
            layuiEntity.setCode("200");
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            layuiEntity.setMsg("用户套餐修改失败");
            layuiEntity.setCode("500");
        }

        return layuiEntity;
    }
}
